package net.rlair.sbdemo.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * mybatis  数据源配置.
 *
 * @author yanghiakun、yuelulan
 */
@Configuration
@MapperScan(basePackages = {"net.rlair.sbdemo.dao.mybatis.bootmapper"}, sqlSessionFactoryRef = "bootSessionFactory")
@MapperScan(basePackages = "net.rlair.sbdemo.dao.mybatis.boottwomapper", sqlSessionFactoryRef = "boottwoSessionFactory")
public class MyBatisDataSourceConfig {

    @Autowired
    private MybatisAutoConfiguration mybatisAutoConfiguration;

    @Autowired
    private Environment env;

    /*数据库1开始*/

    @Primary
    @Bean(name = "bootDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.boot")
    public DataSource bootDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Primary
    @Bean(name = "bootSessionFactory")
    public SqlSessionFactoryBean bootSessionFactory() {
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        sqlSessionFactoryBean.setTypeHandlersPackage(env.getProperty("mybatis.type-handlers-package"));
        sqlSessionFactoryBean.setDataSource(bootDataSource());
        return sqlSessionFactoryBean;
    }

    @Primary
    @Bean(name = "bootTransactionManager")
    public DataSourceTransactionManager bootTransactionManager() {
        return new DataSourceTransactionManager(bootDataSource());
    }

    /*数据库1结束*/

    /*数据库2开始*/

    @Bean(name = "boottwoDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.boottwo")
    public DataSource boottwoDataSource() {
        final BasicDataSource dbSource = (BasicDataSource) DataSourceBuilder.create().type(org.apache.commons.dbcp2.BasicDataSource.class).build();
        dbSource.setMaxIdle(10);
        dbSource.setMaxTotal(50);
        dbSource.setDefaultQueryTimeout(10000);
        return dbSource;

    }

    @Bean(name = "boottwoSessionFactory")
    public SqlSessionFactoryBean boottwoSessionFactory() {
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(boottwoDataSource());
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        sqlSessionFactoryBean.setTypeHandlersPackage(env.getProperty("mybatis.type-handlers-package"));
        return sqlSessionFactoryBean;
    }

    @Bean(name = "boottwoTransactionManager")
    public DataSourceTransactionManager boottwoTransactionManager() {
        return new DataSourceTransactionManager(boottwoDataSource());
    }

    /*数据库2结束*/
}
